package com.campus.merchant.controller;

import com.campus.common.bean.common.Resp;
import com.campus.merchant.service.WebSocketService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * WebSocket测试控制器
 * 用于测试WebSocket功能和查看连接状态
 */
@RestController
@RequestMapping("/websocket-test")
@Api(tags = "WebSocket测试接口")
@Slf4j
public class WebSocketTestController {

    @Autowired
    private WebSocketService webSocketService;

    /**
     * 测试向指定商户发送订单通知
     */
    @PostMapping("/send-order-notification")
    @ApiOperation("测试发送订单通知")
    public Resp<String> sendOrderNotification(
            @ApiParam("商户ID") @RequestParam Integer merchantId,
            @ApiParam("通知消息") @RequestParam String message) {
        try {
            webSocketService.sendOrderNotification(merchantId, message);
            log.info("测试发送订单通知成功，商户ID: {}, 消息: {}", merchantId, message);
            return Resp.success("订单通知发送成功");
        } catch (Exception e) {
            log.error("测试发送订单通知失败: {}", e.getMessage(), e);
            return Resp.error("订单通知发送失败: " + e.getMessage());
        }
    }

    /**
     * 测试广播系统通知
     */
    @PostMapping("/broadcast-system-notification")
    @ApiOperation("测试广播系统通知")
    public Resp<String> broadcastSystemNotification(
            @ApiParam("通知消息") @RequestParam String message) {
        try {
            webSocketService.broadcastSystemNotification(message);
            log.info("测试广播系统通知成功，消息: {}", message);
            return Resp.success("系统通知广播成功");
        } catch (Exception e) {
            log.error("测试广播系统通知失败: {}", e.getMessage(), e);
            return Resp.error("系统通知广播失败: " + e.getMessage());
        }
    }

    /**
     * 获取WebSocket连接统计信息
     */
    @GetMapping("/connection-stats")
    @ApiOperation("获取WebSocket连接统计")
    public Resp<String> getConnectionStats() {
        try {
            String stats = webSocketService.getConnectionStats();
            log.info("获取WebSocket连接统计: {}", stats);
            return Resp.success(stats);
        } catch (Exception e) {
            log.error("获取WebSocket连接统计失败: {}", e.getMessage(), e);
            return Resp.error("获取连接统计失败: " + e.getMessage());
        }
    }

    /**
     * 模拟新订单消息（用于测试）
     */
    @PostMapping("/simulate-new-order")
    @ApiOperation("模拟新订单消息")
    public Resp<String> simulateNewOrder(
            @ApiParam("商户ID") @RequestParam Integer merchantId,
            @ApiParam("订单ID") @RequestParam(required = false, defaultValue = "TEST_ORDER_001") String orderId) {
        try {
            String notification = "您有新的订单，请及时处理！订单ID: " + orderId;
            webSocketService.sendOrderNotification(merchantId, notification);
            log.info("模拟新订单消息发送成功，商户ID: {}, 订单ID: {}", merchantId, orderId);
            return Resp.success("新订单消息模拟成功");
        } catch (Exception e) {
            log.error("模拟新订单消息失败: {}", e.getMessage(), e);
            return Resp.error("新订单消息模拟失败: " + e.getMessage());
        }
    }
}